﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CRM.Database.Model
{
    public class PriceConditionTypesCollection : AbstractCollection
    {
        public List<PriceConditionTypes> PriceConditionTypesList
        {
            get { return Entities.PriceConditionTypes.ToList(); }
        }

        #region Overrides of AbstractCollection

        /// <summary>
        /// Получение списка элементов
        /// </summary>
        /// <returns>Список элементов</returns>
        public override List<object> GetList()
        {
            return new List<object>(PriceConditionTypesList);
        }

        /// <summary>
        /// Получение типа текущего элемента
        /// </summary>
        /// <returns></returns>
        protected override Type GetItemType()
        {
            return typeof (PriceConditionTypes);
        }

        /// <summary>
        /// Получение статического списка для хранения объектов
        /// </summary>
        /// <returns></returns>
        public override object GetStaticCollection()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Получение элемента из коллекции по идентификатору
        /// </summary>
        /// <param name="itemId">Идентификатор</param>
        /// <returns>Элемент</returns>
        public override object GetItemById(int itemId)
        {
            return Entities.PriceConditionTypes.Find(itemId);
        }

        /// <summary>
        /// Обнуление статической коллекции
        /// </summary>
        protected override void SetStaticListToNULL()
        {
            throw new NotImplementedException();
        }

        #endregion

        public new bool Update(object item, string userLogin)
        {
            var newObject = (PriceConditionTypes) item;
            if (newObject == null)
                return false;


            var object4Update = GetItemByIdFromDatabase(newObject.GetKeyValue()) as PriceConditionTypes;
            if (object4Update == null)
                return false;

            if (!string.IsNullOrEmpty(newObject.NameEn)) object4Update.NameEn = newObject.NameEn;
            if (!string.IsNullOrEmpty(newObject.NameRu)) object4Update.NameRu = newObject.NameRu;

            // object4Update.IsActive = newObject.IsActive;

            return base.Update(object4Update, userLogin);
        }
    }
}